<?php $this->beginClip('choose_clearing_agent'); ?>
	<div class="row">
		<?php echo CHtml::link('Back','#',array('id'=>'back'));?>
	</div>
	<div class="row">
	<?php echo CHtml::label("Search",'search_clearing_agent'); ?>
	<?php echo CHtml::textField('search_clearing','',array('grid_id'=>'clearing-agent-grid')); ?>
	</div>

	<div class="form">
		<?php $this->widget('zii.widgets.grid.CGridView', array(
			'id'=>'clearing-agent-grid',
			'dataProvider'=>$company->search(),
			'columns'=>array(
				'cname',
				array(
					'header'=>'Choose',
					'class'=>'CLinkColumn',
					'label'=>'Choose',
					'linkHtmlOptions'=>array('id'=>'select_clearing_agent','input_id'=>'import_clearing_agent_name'),
					'urlExpression'=>'CHtml::normalizeUrl(array("import/create","cargo_id"=>'.$model['cargo']->id.',"clearing_agent_id"=>$data->id))',
				),		
			),
		)); ?>
	</div>
<?php $this->endClip(); ?>
<?php $this->beginClip('choose_loading_agent'); ?>
	
	<div class="row">
		<?php echo CHtml::link('Back','#',array('id'=>'back'));?>
	</div>
	
	<div class="row">
	<?php echo CHtml::label("Search",'search_loading_agent'); ?>
	<?php echo CHtml::textField('search_loading','',array('grid_id'=>'loading-agent-grid')); ?>
	</div>

	<div class="form">
		<?php $this->widget('zii.widgets.grid.CGridView', array(
			'id'=>'loading-agent-grid',
			'dataProvider'=>$company->search(),
			'columns'=>array(
				'cname',
				array(
					'header'=>'Choose',
					'class'=>'CLinkColumn',
					'label'=>'Choose',
					'linkHtmlOptions'=>array('id'=>'select_loading_agent','input_id'=>'import_loading_agent_name'),
					'urlExpression'=>'CHtml::normalizeUrl(array("import/create","cargo_id"=>'.$model['cargo']->id.',"loading_agent_id"=>$data->id))',
				),		
			),
		)); ?>
	</div>
<?php $this->endClip(); ?>
<?php $this->beginClip('form'); ?>
	<div class="form">

	<?php $form=$this->beginWidget('CActiveForm', array(
		'id'=>'import-form',
		'enableAjaxValidation'=>false,
	)); ?>

	<p class="note">Fields with <span class="required">*</span> are required.</p>

	<div class="row">
		<?php echo $form->labelEx($model['import'],'eta'); ?>
		<?php $this->widget('zii.widgets.jui.CJuiDatePicker',
              array(
                    'model'=>$model['import'],
                    'attribute'=>"eta",
                    'htmlOptions'=>array('size'=>47),
                    'options'=>array(
                    	'dateFormat'=>'dd/mm/yy',
              			'value'=>(is_numeric($model['import']->eta))?date('d/m/Y',$model['import']->eta):$model['import']->eta,
              		),
                   )
             );
		?>
		<?php echo $form->error($model['import'],'eta'); ?>
	</div>
	
	<div class="row">
		<?php echo $form->labelEx($model['import'],'jobno'); ?>
		<?php echo $form->textField($model['import'],'jobno',array('size'=>47,'maxlength'=>20,'placeholder'=>'Unique referrence.')); ?>
		<?php echo $form->error($model['import'],'jobno'); ?>
	</div>
	
	<div class="row">
		<?php echo CHtml::label($model['import']->getAttributeLabel('clearing_agent').' - '.CHtml::link('Choose','#',array('id'=>'chooseClearing','panel_id'=>'clearing_agent')),'clearing_agent'); ?>
		<?php echo $form->textField($model['import'],'clearing_agent_name',array('size'=>47,'maxlength'=>50,'data_id'=>$model->clearing_agent,'disabled'=>'disabled')); ?>
		<?php echo $form->error($model['import'],'clearing_agent_name'); ?>
	</div>
	
	<div class="row">
		<?php echo CHtml::label($model['import']->getAttributeLabel('loading_agent').' - '.CHtml::link('Choose','#',array('id'=>'chooseLoading','panel_id'=>'loading_agent')),'loading_agent'); ?>
		<?php echo $form->textField($model['import'],'loading_agent_name',array('size'=>47,'maxlength'=>50,'data_id'=>$model->loading_agent,'disabled'=>'disabled')); ?>
		<?php echo $form->error($model['import'],'loading_agent_name'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model['import'],'pol'); ?>
		<?php echo $form->textField($model['import'],'pol',array('size'=>47,'maxlength'=>50,'placeholder'=>'Country origin / Port of Loading')); ?>
		<?php echo $form->error($model['import'],'pol'); ?>
	</div>
	
	<div class="row">
		<?php echo $form->labelEx($model['import'],'pod'); ?>
		<?php echo $form->textField($model['import'],'pod',array('size'=>47,'maxlength'=>50,'placeholder'=>'Distination / Port of Discharge')); ?>
		<?php echo $form->error($model['import'],'pod'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model['import'],'freight_info'); ?>
		<?php echo $form->textField($model['import'],'freight_info',array('size'=>47,'maxlength'=>50,'placeholder'=>'Fright reference / Vessel reference')); ?>
		<?php echo $form->error($model['import'],'freight_info'); ?>
	</div>

	<div class="row">
		<table class="group" id="ref" cellpadding="0" cellspacing="0" border="0">
			<thead>
				<tr>
					<td><?php echo CHtml::label($model['courier'][0]->getAttributeLabel('ref_no').' - '.CHtml::link('Add','#',array('id'=>'add')),'courier_ref'); ?></td>
				</tr>
			</thead>
			<tbody>
			<?php foreach($model['courier'] as $i=>$item): ?>
				<tr>
					<td>
						<?php echo CHtml::activeDropDownList($item, "[$i]ref_type", $item->type,array('style'=>'width:119px;')); ?>
                		<?php echo CHtml::activeTextField($item,"[$i]ref_no",array('size'=>30,'rows'=>3,'placeholder'=>'Import referrence')); ?>
                		<?php
           					if($i>0)
           						echo CHtml::link('remove','#',array('id'=>'remove','controlRow'=>1)) 
           				?>  
					</td>
                </tr>
            <?php endforeach; ?>
            </tbody>
            </table>
	</div>

	<div class="row buttons">
		<?php 
			$cancelUrl = Yii::app()->user->getReturnUrl();
        	$this->widget('zii.widgets.jui.CJuiButton',array(
        		'name'=>'submit',
        		'caption'=>'Add info',
        		'value'=>"create",
        	));
        	$this->widget('zii.widgets.jui.CJuiButton',array(
        		'name'=>'cancel',
        		'caption'=>'Cancel',
        		'value'=>"cancel",
        		'buttonType'=>'button',
        		'onclick'=>"js:function(){window.location = '$cancelUrl';return false;}"
        	));  
        ?>
	</div>

	<?php $this->endWidget(); ?>

	</div>
<?php $this->endClip(); ?>
<?php $this->widget('application.widgets.wiNerdy.panel',array(
	'id'=>'import_panel',
	'panel'=>array(
		'form'=>$this->clips['form'],
		'clearing_agent'=>$this->clips['choose_clearing_agent'],
		'loading_agent'=>$this->clips['choose_loading_agent'],
	)
))?>

<?php
$cs = Yii::app()->ClientScript;
$cargoViewURL = CHtml::normalizeUrl(array('cargo/view','id'=>$model['cargo']->id));
$companyGridURL = CHtml::normalizeUrl(array('import/chooseshipping','cargo_id'=>$model['cargo']->id));
$script =<<<EOD
$.import_create_form = {
	afterSubmit:function(data){
		window.location.href = "$cargoViewURL#import";
		window.location.reload(true);
	}
};
$('#import-form [type=submit]').click(function(){
	var params = [];
	var agent = $('#import_clearing_agent_name').attr('data_id');
	var name = "import[clearing_agent]";
	if(agent == undefined)
		params[name] = 0;
	else
		params[name] = agent;
	var agent = $('#import_loading_agent_name').attr('data_id');
	var name = "import[loading_agent]";
	if(agent == undefined)
		params[name] = 0;
	else
		params[name] = agent;
	var form = $.extend($.simplelogform,$.import_create_form);
	form.Submit($('#import-form'),params);
	return false;
});
$('a#add').click(function(){
	var table = $(this).parents('table');
	AddNewTableRow(table);
	return false;
});
$('select').live('change',function(){
	if($(this).val() == "Other"){
		var html = "<input size='13' name='"+$(this).attr('name')+"' id='"+$(this).attr('id')+"' placeholder='Please specify' />";
		$(this).replaceWith(html);
	}
});
$('input#search_clearing, input#search_loading').keypress(function(event){
	if(event.keyCode == '13'){
		$.fn.yiiGridView.update($(this).attr('grid_id'),{data:{searchText:$(this).val()}});
	}
});
$('a#select_clearing_agent, a#select_loading_agent').live('click',function(){
	var row = $(this).parents('tr').index();
	var id = $.fn.yiiGridView.getKey($(this).parents('.grid-view').attr('id'),row);
	var name = $(this).parents('tr').children('td').eq(0).html();
	$('#'+$(this).attr('input_id')).val(name);
	$('#'+$(this).attr('input_id')).attr('data_id',id);
	$.winerdyPanel.MoveById('import_panel','form');
	return false;
});
$('a#back').click(function(){
	$.winerdyPanel.MoveById('import_panel','form');
	return false;
});
$('a#chooseLoading, a#chooseClearing').click(function(){
	$.winerdyPanel.MoveById('import_panel',$(this).attr('panel_id'));
	return false;
});
EOD;
$cs->registerScript('import#create#form',$script);
?>